package com.training.hatton.dropboxclient.adapter;

import android.content.Context;

import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.training.hatton.dropboxclient.R;
import com.training.hatton.dropboxclient.content.contract.MetadataContract;
import com.training.hatton.dropboxclient.images.ImageLoader;
import com.training.hatton.dropboxclient.utill.DropboxService;

public class FileListCursorAdapter extends CursorAdapter {

	public FileListCursorAdapter(Context context, Cursor c) {
		super(context, c, true);
	}

	@Override
	public void bindView(View view, Context ctx, final Cursor cursor) {
		final TextView tvPath = (TextView) view.findViewById(R.id.adpt_name);
		tvPath.setText(cursor.getString(cursor.getColumnIndex(MetadataContract.MetadataColumns.NAME)));
		ImageView imageView = (ImageView) view.findViewById(R.id.adpt_image);

		if(cursor.getInt(cursor.getColumnIndex(MetadataContract.MetadataColumns.THUMB_EXISTS)) > 0){
			imageView.setImageResource(android.R.drawable.stat_notify_error);
			String url = DropboxService.THUMBNAILS_BASE_URL + DropboxService.ROOT + 
					cursor.getString(cursor.getColumnIndex(MetadataContract.MetadataColumns.PATH))
					.replace(" ", "%20").replace("[", "%5B").replace("]", "%5D") + "?" + "size=s";
			ImageLoader.get(mContext).bind(url, imageView, this);
		}else{
			if (cursor.getInt(cursor.getColumnIndex(MetadataContract.MetadataColumns.IS_DIR)) > 0){
				imageView.setImageResource(R.drawable.folder48);
			}else{
				imageView.setImageResource(R.drawable.page_white48);
			}
		}
	}
	
	@Override
	public View newView(Context ctx, Cursor cursor, ViewGroup arg2) {
		View view = View.inflate(ctx, R.layout.adapter_file_list_element, null);
		return view;
	}

}
